package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.myapplication.databinding.FragmentHomeBinding;

import java.util.List;


public class HomeFragment extends Fragment {
    private FragmentHomeBinding binding;
    private UserViewModel userViewModel;
    private UserAdapter userAdapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        initViews();
        initViewModel();
        initRecyclerView();
        initSwipeRefresh();
        userViewModel.fetchUserList();
        return binding.getRoot();
    }


    private void initViews() {
        SwipeRefreshLayout srlRefresh = binding.srlUserRefresh;
        RecyclerView rvUserList = binding.rvUserList;
    }


    private void initViewModel() {
        userViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
        userViewModel.getUserListLiveData().observe(getViewLifecycleOwner(), this::updateUserList);
        userViewModel.getIsLoadingLiveData().observe(getViewLifecycleOwner(), isLoading -> {
            binding.srlUserRefresh.setRefreshing(isLoading);
        });
    }


    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvUserList.setLayoutManager(layoutManager);
        userAdapter = new UserAdapter(null);
        binding.rvUserList.setAdapter(userAdapter);
    }


    private void initSwipeRefresh() {
        binding.srlUserRefresh.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light
        );
        binding.srlUserRefresh.setOnRefreshListener(() -> userViewModel.fetchUserList());
    }


    private void updateUserList(List<UserResponse.User> userList) {
        if (userList != null && !userList.isEmpty()) {
            userAdapter.setUserList(userList);
            binding.rvUserList.setVisibility(View.VISIBLE);
        } else {
            binding.rvUserList.setVisibility(View.GONE);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
        private List<UserResponse.User> userList;

        public UserAdapter(List<UserResponse.User> userList) {
            this.userList = userList;
        }

        public void setUserList(List<UserResponse.User> userList) {
            this.userList = userList;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.activity_game_circle, parent, false); // 你的卡片布局
            return new UserViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
            if (userList == null) return;

            // 卡片点击事件：区分跳转目标
            holder.itemView.setOnClickListener(v -> {
                if (position == 0) { // 第一个卡片跳转到“游戏圈子”
                    Intent intent = new Intent(requireContext(), GameCircleActivity.class);
                    startActivity(intent);
                } else if (position == 1) { // 第二个卡片跳转到“游戏IP线下活动”
                    Intent intent = new Intent(requireContext(), EventListActivity.class);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return userList == null ? 0 : userList.size();
        }

        class UserViewHolder extends RecyclerView.ViewHolder {
            public UserViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }
}